package com.example.JPetStore.controller;

import com.example.JPetStore.Common;
import com.example.JPetStore.dao.OrderDao;
import com.example.JPetStore.domain.Order;
import com.example.JPetStore.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@RequestMapping("/order")
@Controller
public class OrderController {
    @Autowired
    OrderDao orderDao;
    @GetMapping("/OrderController")
    public ModelAndView order(HttpSession session){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;


        ArrayList<Order> orders= orderDao.queryOrders(user);

        mv.addObject("orders",orders);
        mv.setViewName("/order/order");
        return mv;
    }

    @GetMapping("/OrderDetailController")
    public ModelAndView orderDetail(HttpSession session, @RequestParam("order_id")String orderId){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;


        Order order=orderDao.queryOrderById(user,orderId);

        if(order==null){
            Common.sendErrorMessage(mv, "订单不存在！", "/order/orderController");
            return mv;
        }

        mv.addObject("order",order);
        mv.setViewName("/order/orderdetail");
        return mv;
    }
}
